package com.uam.core.entity.uitls;



import com.uam.core.entity.constant.BaseErrorCode;
import com.uam.core.entity.enums.TrackErrorCode;
import com.uam.core.lk.utils.ResponseResult;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;



/**
 * @Description 请求结果封装
 * @Author heguanglong
 * @Date 2019-02-19 10:54
 **/
@Data
@ApiModel("通用的数据返回对象")
public class Result<T> {

    @ApiModelProperty("返回状态：1失败，0成功")
    private Integer status;
    @ApiModelProperty("返回提示信息")

    private String message;
    @ApiModelProperty("返回的数据对象")

    private T result;

    public Result() {
        this.setStatus(ResponseResult.SUCCESS);
        this.setMessage("success");
    }

    public Result(String errorMsg,Integer code) {
        this.setStatus(code == null ? ResponseResult.ERROR:code);
        this.setMessage(errorMsg);
    }

    public Result(T data) {
        this.setStatus(ResponseResult.SUCCESS);
        this.setMessage("success");
        this.setResult(data);
    }


    public Result(ResponseResult result) {
        this.setStatus(result.getCode());
        this.setMessage("success");
        this.setResult((T)result.getData());
    }

    /**
     * 调用返回自定义的信息，无数据返回前端
     * @param <T>
     * @date 2019/10/10 17:37
     * @modify heguanglong
     */
    public static <T> Result<T> build(int status,String msg) {
        Result rs = new Result<>();
        rs.setStatus(status);
        rs.setMessage(msg);
        return rs;
    }

    public static Result build(TrackErrorCode pbfBlockErrorCode) {
        Result rs = new Result<>();
        rs.setStatus(pbfBlockErrorCode.getErrorCode());
        rs.setMessage(pbfBlockErrorCode.getErrorMessage());
        return rs;
    }

    public static Result build(BaseErrorCode springExceptionErrorCode) {
        Result rs = new Result<>();
        rs.setStatus(springExceptionErrorCode.getErrorCode());
        rs.setMessage(springExceptionErrorCode.getErrorMessage());
        return  rs;
    }

}
